package com.example.demo.controller;

import com.example.demo.config.DynamicDataSourceContextHolder;
import com.example.demo.entity.CcCommodityPrice;
import com.example.demo.entity.TGdCommodityExtend;
import com.example.demo.service.CcCommodityPriceService;
import com.example.demo.service.TGdCommodityExtendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author makejava
 * @since 2021-09-13 20:29:09
 */
@RestController
@RequestMapping("/test")
public class TestController {

    Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    private CcCommodityPriceService ccCommodityPriceService;

    @Resource
    private TGdCommodityExtendService gdCommodityExtendService;

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("/all")
    public List<CcCommodityPrice> all() {
        for (int i = 0; i < 100; i += 50) {
            DynamicDataSourceContextHolder.setDataSourceKey("slave");
            logger.info("当前数据源[{}]", DynamicDataSourceContextHolder.getDataSourceKey());
            List<CcCommodityPrice> ccCommodityPrices = ccCommodityPriceService.queryAllByLimit(i, 50);
            logger.info("获取数据[{}]条", ccCommodityPrices.size());
            DynamicDataSourceContextHolder.setDataSourceKey("master");
            logger.info("当前数据源[{}]", DynamicDataSourceContextHolder.getDataSourceKey());
            ccCommodityPriceService.insertBatch(ccCommodityPrices);
        }
        return null;
    }

    @GetMapping("/update")
    public void update() {
        DynamicDataSourceContextHolder.setDataSourceKey("master");
        logger.info("当前数据源[{}]", DynamicDataSourceContextHolder.getDataSourceKey());
        List<CcCommodityPrice> ccCommodityPrices = ccCommodityPriceService.queryAllByLimit(0, 50);
        ccCommodityPrices.forEach(item -> {
            item.setStartDate(new Date());
        });
        ccCommodityPrices.forEach(System.out::println);
        long l = ccCommodityPriceService.insertOrUpdateBatch(ccCommodityPrices);
        logger.info("受影响行数[{}]", l);

    }


    @GetMapping("/test/{num}")
    public void test(@PathVariable int num) {
        List<TGdCommodityExtend> commodityExtendList = new LinkedList<>();
        TGdCommodityExtend commodityExtend1 = new TGdCommodityExtend();
        TGdCommodityExtend commodityExtend2 = new TGdCommodityExtend();
        TGdCommodityExtend commodityExtend3 = new TGdCommodityExtend();
        commodityExtend1.setCommodityCode("G1");
        commodityExtend2.setCommodityCode("G2");
        commodityExtend3.setCommodityCode("G3");
        if (1 == num) {

            commodityExtend1.setOffShelfDate(new Date());
            commodityExtend1.setOffShelfUserCode("下架人1");
            commodityExtend1.setOffShelfUserName("下架人1");

            commodityExtend2.setOffShelfDate(new Date());
            commodityExtend2.setOffShelfUserCode("下架人2");
            commodityExtend2.setOffShelfUserName("下架人2");

            commodityExtend3.setOffShelfDate(new Date());
            commodityExtend3.setOffShelfUserCode("下架人3");
            commodityExtend3.setOffShelfUserName("下架人3");


        }

        if (2 == num) {

            commodityExtend1.setOnShelfDate(new Date());
            commodityExtend1.setOnShelfUserCode("上架人1");
            commodityExtend1.setOnShelfUserName("上架人1");

            commodityExtend2.setOnShelfDate(new Date());
            commodityExtend2.setOnShelfUserCode("上架人2");
            commodityExtend2.setOnShelfUserName("上架人2");


            commodityExtend3.setOnShelfDate(new Date());
            commodityExtend3.setOnShelfUserCode("上架人3");
            commodityExtend3.setOnShelfUserName("上架人3");
        }

        if (3 == num) {

            commodityExtend1.setReviewDate(new Date());
            commodityExtend1.setReviewUserCode("审核人1");
            commodityExtend1.setReviewUserName("审核人1");

            commodityExtend2.setReviewDate(new Date());
            commodityExtend2.setReviewUserCode("审核人2");
            commodityExtend2.setReviewUserName("审核人2");


            commodityExtend3.setReviewDate(new Date());
            commodityExtend3.setReviewUserCode("审核人3");
            commodityExtend3.setReviewUserName("审核人3");
        }

        commodityExtendList.add(commodityExtend1);
        commodityExtendList.add(commodityExtend2);
        commodityExtendList.add(commodityExtend3);
        logger.info("当前数据源[{}]", DynamicDataSourceContextHolder.getDataSourceKey());
        Long aLong = gdCommodityExtendService.insertOrUpdateBatch(commodityExtendList);
        logger.info("受影响行数[{}]", aLong);
    }


}
